/* 
* Authors: Karymov Anton
*		   Skorodumov Kirill
* This file contains tests and wrote test result in test_log.txt
* June-August,	2011
*/

#include <iostream> 
#include <fstream>
#include <stdio.h>
#include <time.h>
#include "branching_algorythm.h"
#include "types.h"
#include "graph_reader.h"
#include "max_degree_algorythm.h"
#include "performance_test.h"
#include <math.h>

using namespace std;

int performance_test(const int k) 
{
  clock_t start, end;
  const int optimization_level = 1;
  const int MAX_FILENAME_LENGTH = 256;
  const std::string TEST_LOG = "Tests/test_log.txt";
  const int number_of_tests = 6;
  char file_name[MAX_FILENAME_LENGTH];
  char* root = "Tests/test_0";
  char* ext = ".dot";

  std::ofstream file(TEST_LOG.c_str());
  file<<"+-----------------------+-------------------------------------------------+-----------------+----------------------------------------------+------------------+------------------------+"<< std::endl;
  file<<"| Number of nodes       |                      Time                       | Number of nodes |                     Time                     |  Number of nodes |          Time          |"<< std::endl;
  file<<"+-----------------------+-------------------------------------------------+-----------------+----------------------------------------------+------------------+------------------------+"<< std::endl;
  file<<"| Graph  | Vertex Cover |   Parametrized Algorithm without optimization   |  Vertex Cover   |   Parametrized Algorithm with optimization   |  Vertex Cover    |  Max Degree Algorithm  |"<< std::endl;
  file<<"+--------+--------------+-------------------------------------------------+-----------------+----------------------------------------------+------------------+------------------------+"<< std::endl;
  for (int i = 1; i <= number_of_tests; i++)
  {
    sprintf_s(file_name, "%s%d%s", root, i, ext);
    graph_t graph = read_graph(file_name);
    branching_algorythm branchingSolver(graph);

    int number_of_vertices = branchingSolver.number_of_vertices();
    file<<"| "<< number_of_vertices;
    const int shift_first_column = 7;
    print_space(shift_first_column-int_to_string(number_of_vertices).length(),file);		

    start = clock();
    branchingSolver.solve(k);
    end = clock();

    int branching_algorythm_cover_size = branchingSolver.get_cover_size();
    file<< "     " << branching_algorythm_cover_size;
    const int shift_second_column = 9;
    print_space(shift_second_column-int_to_string(branching_algorythm_cover_size).length(),file);		

    double branching_algorythm_work_time = (double)(end - start)/CLOCKS_PER_SEC;
    file<<"   " << branching_algorythm_work_time;
    const int shift_third_column = 46;
    print_space(shift_third_column-double_to_string(branching_algorythm_work_time).length(),file);

    branching_algorythm branchingSolver_(graph);
    branchingSolver_.set_optimization_level(optimization_level);
    start = clock();
    branchingSolver_.solve(k);
    end = clock();

    int opt_branching_algorythm_cover_size = branchingSolver_.get_cover_size();
    file<< "  " << opt_branching_algorythm_cover_size;
    const int shift_fourth_column = 15;
    print_space(shift_fourth_column-int_to_string(opt_branching_algorythm_cover_size).length(),file);		

    double opt_branching_algorythm_work_time = (double)(end - start)/CLOCKS_PER_SEC;
    file<<"   " << opt_branching_algorythm_work_time;
    const int shift_fifth_column = 43;
    print_space(shift_fifth_column-double_to_string(opt_branching_algorythm_work_time).length(),file);

    max_degree_algorythm Max_degree_solver(graph);
    start = clock();			
    Max_degree_solver.greedy_min_vertex_cover();
    end = clock();

    int max_degree_algorythm_cover_size = Max_degree_solver.get_cover().size(); 
    file<< "  " << max_degree_algorythm_cover_size;
    const int shift_sixth_column = 16;
    print_space(shift_sixth_column-int_to_string(max_degree_algorythm_cover_size).length(),file);

    double max_degree_algorythm_work_time = (double)(end - start)/CLOCKS_PER_SEC;
    file<<"  " << max_degree_algorythm_work_time; 
    const int shift_seventh_column = 22;
    print_space(shift_seventh_column-double_to_string(max_degree_algorythm_work_time).length(),file);		
    file<<std::endl;

    file<<"+--------+--------------+-------------------------------------------------+-----------------+----------------------------------------------+------------------+------------------------+"<< std::endl;

    std::cout << file_name << " complete." << std::endl;
    file_name[0] = '\0';
  }
  file.close();
  return 0;
}



